﻿using System;
using System.Security.AccessControl;
using Microsoft.Win32;

namespace MathService.Extentions
{
    public static class RegistryKeyExtentions
    {
        public static void SetAccessRights(this RegistryKey Key) { SetAccessRights(Key, Environment.UserName); }


        public static void SetAccessRights(this RegistryKey Key, string identity)
        {
            identity = string.Format(@"{0}\{1}", Environment.UserDomainName, identity);

            var mSec = new RegistrySecurity();
            var rule = new RegistryAccessRule(
                identity,
                RegistryRights.ReadKey,
                InheritanceFlags.None,
                PropagationFlags.None,
                AccessControlType.Allow);
            mSec.AddAccessRule(rule);
            Key.SetAccessControl(mSec);
        }
    }
}
